package org.dev_common.file;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

/**
 * a simple file modifying listener.
 */
public class FileConfigListener /* implements ConfigListener<String> */ {

	// @Override
	public boolean isReload() {
		return false;
	}

	// @Override
	public void load() throws Exception {

		WatchService watchService = FileSystems.getDefault().newWatchService();
		Path path = Paths.get("test");
		WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
		WatchKey key;
		while ((key = watchService.take()) != null) {
			for (WatchEvent<?> event : key.pollEvents()) {
				// process
				// System.out.println(JsonUtils.bean2Json(event));
				System.out.println(String.format("%s,%s", event.kind(), event.context()));
			}
			key.reset();
		}
	}

	public static void main(String[] args) throws Exception {
		new FileConfigListener().load();
	}

}
